﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimTank
{
    public partial class BaseMob
    {
        protected virtual void observedTimeToLive(int damage)
        {
            if (damage == 0) { return; }
            int[] newTTLtracker = { damage, timestamp };
            if (damage > health)
            {
                updatePreviousTTL(0, damage);
            }
            else
            {
                observedTTL.Add(newTTLtracker);
            }
            int queueLength = observedTTL.Count - 2;
            for (int i = queueLength; i >= 0; i--)
            {
                int[] temp;
                temp = observedTTL[i];
                temp[0] = temp[0] + damage;
                if (temp[0] > health)
                {
                    //damage done is greater than HP
                    int timeTaken, killDamage;
                    timeTaken = timestamp - temp[1];
                    killDamage = temp[0];
                    //add this kill to the list of kills in the proper position
                    updatePreviousTTL(timeTaken, killDamage);
                    //kill has been recorded, remove from the current queue
                    observedTTL.RemoveAt(i);
                }
                else
                {
                    //damage done is less than HP
                    observedTTL[i] = temp;
                }
            }
        }

        protected void updatePreviousTTL(int timeTaken, int damage)
        {
            //we have to search the existing TTL queue for a kill time equal to the one passed.
            bool match = false;
            int j = 0;
            if (TTLStatVector.Count == 0)  //this is the first entry, just need to add a single record to avoid invalid indexes
            {
                int[] temp = { 0, 0 };
                TTLStatVector.Add(temp);
            }

            do  //search through the record of previous kills for anotehr that had the same duration
            {
                if (TTLStatVector[j][0] == timeTaken)
                {
                    TTLStatVector[j][1]++;  //if we find out, increase the count and note the match
                    match = true;
                }
                else if (TTLStatVector[j][0] == 0)  //the last entry has a count of 0, so if we get there, replace that record with the current one and add another 0,0
                {
                    TTLStatVector[j][0] = timeTaken;
                    TTLStatVector[j][1] = 1;
                    int[] temp = { 0, 0 };
                    TTLStatVector.Add(temp);
                    match = true;
                }
                j++;
            } while (match == false && j < TTLStatVector.Count);

            //check for no matches
            if (match == false)
            {
                //no matches, new kill time
                int[] temp = { timeTaken, 1 };
                TTLStatVector.Add(temp);
            }

            if (timeTaken < LowestTTL)  // we have a new record lowest time
            {
                LowestTTL = timeTaken;
                LowestTTLcount = 1;
                LowestTTLdamage = damage;
            }
            else if (timeTaken == LowestTTL) //already seen this lowest time
            {
                LowestTTLcount++;
            }
        }

        public int mean
        {
            get
            {
                int count = 0;
                int weight = 0;
                foreach (int[] item in TTLStatVector)
                {
                    weight += item[0] * item[1];
                    count += item[1];
                }
                return (int)(weight / count);
            }
        }

        public double stddev
        {
            get
            {
                int count = 0;
                int sum = 0;
                foreach (int[] item in TTLStatVector)
                {
                    count += item[1];
                }
                foreach (int[] item in TTLStatVector)
                {
                    sum += (int)(Math.Pow(item[0] - mean, 2) * item[1] / count);
                }
                return Math.Pow(sum, .5);
            }
        }
    }
}
